package com.ruoyi.base.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.base.domain.Inventory;
import com.ruoyi.base.service.IInventoryService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 库存信息Controller
 *
 * @author wonderland
 * @date 2023-07-16
 */
@RestController
@RequestMapping("/inventory")
public class InventoryController extends BaseController {
    @Autowired
    private IInventoryService inventoryService;

    /**
     * 查询库存信息列表
     */
    @RequiresPermissions("base:inventory:list")
    @GetMapping("/list")
    public TableDataInfo list(Inventory inventory) {
        startPage();
        List<Inventory> list = inventoryService.selectInventoryList(inventory);
        return getDataTable(list);
    }

    /**
     * 导出库存信息列表
     */
    @RequiresPermissions("base:inventory:export")
    @Log(title = "库存信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Inventory inventory) {
        List<Inventory> list = inventoryService.selectInventoryList(inventory);
        ExcelUtil<Inventory> util = new ExcelUtil<Inventory>(Inventory.class);
        util.exportExcel(response, list, "库存信息数据");
    }

    /**
     * 获取库存信息详细信息
     */
    @RequiresPermissions("base:inventory:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(inventoryService.selectInventoryById(id));
    }

    @GetMapping(value = "/getByBarCode/{code}")
    public Inventory getByBarCode(@PathVariable("code") String code) {
        return inventoryService.getByBarCode(code);
    }



    /**
     * 新增库存信息
     */
    @RequiresPermissions("base:inventory:add")
    @Log(title = "库存信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Inventory inventory) {
        return toAjax(inventoryService.insertInventory(inventory));
    }

    @PostMapping("/insert")
    public AjaxResult insert(@RequestBody Inventory inventory) {
        return toAjax(inventoryService.insertInventory(inventory));
    }

    /**
     * 修改库存信息
     */
    @RequiresPermissions("base:inventory:edit")
    @Log(title = "库存信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Inventory inventory) {
        return toAjax(inventoryService.updateInventory(inventory));
    }

    @PutMapping("/updateInventory")
    public AjaxResult updateInventory(@RequestBody Inventory inventory) {
        return toAjax(inventoryService.updateInventory(inventory));
    }

    /**
     * 删除库存信息
     */
    @RequiresPermissions("base:inventory:remove")
    @Log(title = "库存信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(inventoryService.deleteInventoryByIds(ids));
    }
}
